package com.handu.luna.boss.controller;


import com.handu.luna.core.domain.system.Deliver;
import com.handu.luna.core.domain.system.DeliverArea;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wangzailiang on 2014/7/17.
 */
@Controller
@RequestMapping("/system/deliver/*")
public class DeliverController extends BaseController {

    @RequestMapping
    public BossModel index(String viewState,@PageableDefault(page = 0, size = DEFAULT_PAGE_SIZE) Pageable pageable) {

        String state = "";
        if (viewState != null) {
            state = viewState;
        }

        return createModel("/system/deliver/index")
                .add("deliverPage", context.getDeliverRepository().findAll(pageable))
                .add("viewState",state);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public BossModel add() {

        Deliver deliver = new Deliver();
        //deliver.setType(Deliver.DeliverType.OP);

        return getEditModel(deliver);
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public BossModel edit(@PathVariable long id) {

        return getEditModel(context.getDeliverRepository().findOne(id));
    }

    @RequestMapping(value = "setArea/{id}", method = RequestMethod.GET)
    public BossModel setArea(@PathVariable long id) {

        Deliver deliver = context.getDeliverRepository().findOne(id);

        List<DeliverArea> filterDeliverArea = deliver.filterAreas();

        return createModel("/system/deliver/setarea")
                  .add("deliverAreaList",filterDeliverArea)
                  .add("deliver",deliver);
    }

    @RequestMapping(value = "saveAreas", method = RequestMethod.POST)
    public BossModel saveAreas(long id,Long[] deliverAreaIdList,RedirectAttributes redirectAttributes) {

        Deliver deliver = context.getDeliverRepository().findOne(id);
        deliver.setDeliverAreas(null);
        if(null != deliverAreaIdList) {
            deliver.setDeliverAreaIdList(Arrays.asList(deliverAreaIdList));
        }else {
            deliver.setDeliverAreaIdList(null);
        }
        deliver.update();
        redirectAttributes.addFlashAttribute("message","设置 配送区域 成功");

        return createModel("redirect:index");
    }


    @InitBinder(value = {"name", "description","type","deliverAreas","displayed"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BossModel save(@Valid Deliver deliver, BindingResult bindingResult, RedirectAttributes redirectAttributes) {

        if(CollectionUtils.isEmpty(deliver.getDeliverTypeList())) {
            bindingResult.rejectValue("deliverTypeList","","请选择支付方式");
        }

        if(bindingResult.hasErrors()) {
            return getEditModel(deliver);
        }

        if (deliver.isNew()) {
            context.getDeliverRepository().save(deliver);
            redirectAttributes.addFlashAttribute("message","添加 配送方式 成功");
        } else {
            deliver.update();
            redirectAttributes.addFlashAttribute("message","修改 配送方式 成功");
        }

        return createModel("redirect:index");
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BossModel delete(String viewState,long id, @PageableDefault(page = 0, size = DEFAULT_PAGE_SIZE) Pageable pageable) {

        context.getDeliverRepository().delete(id);

        return createModel("/system/deliver/deliver_table")
                .add("deliverPage", context.getDeliverRepository().findAll(pageable))
                .add("viewState",viewState);
    }


    private BossModel getEditModel(Deliver deliver) {

        BossModel bossModel = createModel("/system/deliver/info")
                .add("deliver", deliver)
                .add("typeList",Deliver.DeliverType.values());

        return bossModel;
    }


}
